package com.hadoop.mr.reducer;

import com.hadoop.mr.mapper.CommentMonthCountMapper;
import com.hadoop.mr.writable.ClientWritable;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by Administrator on 2019/7/11.
 */
public class ClientCountReducer extends Reducer<Text, ClientWritable, ClientWritable, ClientWritable> {

    private static Logger logger = LoggerFactory.getLogger(ClientCountReducer.class);

    @Override
    protected void reduce(Text key, Iterable<ClientWritable> values, Context context)
            throws IOException, InterruptedException {
      try {
          Integer goodsSum = 0;
          String goodsType = "";
          String client = "";
          String goodsName = "";
          for (ClientWritable bean : values) {
              if (StringUtils.isBlank(goodsType)) {
                  goodsType = bean.getGoodsType();
              }
              if (StringUtils.isBlank(client)) {
                  client = bean.getClient();
              }
              if (StringUtils.isBlank(goodsName)) {
                  goodsName = bean.getGoodsName();
              }
              goodsSum += bean.getCount();
          }
          context.write(new ClientWritable(new Text(key).toString(), goodsName, goodsType, client, goodsSum), null);
      }catch (Exception e){
          logger.error("", e);
      }
    }


}